<?php

/**
 * Description of VirtualLabs_Admin_Machines
 *
 * @author markos
 */
class Capacity {

    const CAPACITY_TYPE_MEMORY = 0;
    const CAPACITY_TYPE_CPU = 1;
    const CAPACITY_TYPE_STORAGE = 2;

    private $log;

    public function __construct($log) {
        $this->log = $log;
    }

    public function listCapacity() {
        try {
            $url = array();
            $url["response"] = "json";
            $url["command"] = "listCapacity";
            $config = new Zend_Config_Ini(CONFIGURATION_FILE, APPLICATION_ENV);
            $url["zoneid"] = $config->get("zone_id");
            $v = new HttpClient($this->log);
            $response = $v->call($url);
            $listCapacityResponse = Zend_Json_Decoder::decode($response);

            if (isset($listCapacityResponse['listcapacityresponse']['count'])) {
                if ($listCapacityResponse['listcapacityresponse']['count'] > 0) {
                    $capacityResponse = array();
                    foreach ($listCapacityResponse['listcapacityresponse']['capacity'] as $c) {
                        switch ($c['type']) {
                            case self::CAPACITY_TYPE_CPU:
                                $capacityResponse['cpu'] = round($c['percentused']);
                                break;
                            case self::CAPACITY_TYPE_MEMORY:
                                $capacityResponse['memory'] = round($c['percentused']);
                                break;
                            case self::CAPACITY_TYPE_STORAGE:
                                $capacityResponse['storage'] = round($c['percentused']);
                                break;
                            default:
                                break;
                        }
                    }
                }
                return $capacityResponse;
            }
        } catch (Exception $e) {
            $this->log->err($e->getTraceAsString());
            throw $e;
        }
    }

}

?>
